<template>
	<van-list
	  v-model="loading"
	  :finished="finished"
	  finished-text="没有更多了"
	  @load="onLoad"
	>
	  <!-- <van-cell v-for="(item,index) in list" :key="index" :title="item.content" /> -->
	  <comment-item 
		:comment = "item" 
		v-for="(item,index) in commenArr" 
		:key="index"
		@postComment = "$emit('click-reply',item)"
		></comment-item>
	</van-list>
</template>

<script>
	import {getCommentList} from '@/api/comment.js'
	import commentItem from './commentItem.vue'
	export default {
		data(){
			return {
				
				loading: false,
				finished: false,
				offset:null,
				limit:10
				
			}
		},
		props:{
			art_id:{
				type:[Number,String,Object],
				required:true
			},
			commenArr:{
				type:Array,
				default(){
					return []
				}
			},
			totalCount:{
				type:Number
				
			},
			type:{
				type:String,
				default:'a'
			}
		},
		model:{
			prop:'commenArr',
			event:'input'
		},
		components:{
			commentItem
		},
		methods:{
			async onLoad() {
				
			      try{
					 const {data} = await getCommentList({
						 type:this.type,
						 source:this.art_id.toString(),
						 offset:this.offset,
						 limit:this.limit
					 }) 
					
					 const {results,total_count:totalCount} = data.data
					 
					let arr = this.commenArr.slice()
					arr.push(...results)
					
					this.$emit("input",arr)
					this.$emit('update:total-count',totalCount)
					this.loading= false
					
					if(results.length){
						this.offset = data.data.last_id
					}else{
						
						this.finished = true
					}
				  }catch(err){
					  console.log(err)
				  }
			}
		}
	}
</script>

<style scoped lang="less">
</style>
